var $ = require('../../../utils/helper.js'), that = null
var app = getApp()
Page({
  data: {
    areaindex: [0, 0],
    sec_id: 2,
    fir_id: 1,
    cate_id: 0
  },
  onLoad(e) {
    that = this
  },
  onShow: function () {
    that.setData({ serarc_name: null })
    that.setData({ issearch: 0 })
    let _cate_id = wx.getStorageSync("service_cate_id");
    if (_cate_id != null) {
      that.setData({
        cate_id: _cate_id
      })
    }
    wx.removeStorageSync("service_cate_id");
    $.check(that)
  },

  init: function () {
    $.post("api/service/cate_lists", null, function (cate_list) {
      that.setData({ cate_list: cate_list })
    })
    wx.getLocation({
      success: function (res) {
        let _loaction = res.latitude + "," + res.longitude;
        $.post("api/index/getlocation", { location: _loaction }, function (data) {
          that.setData({ fir_id: data.fir, sec_id: data.sub })
          that.setarealist()
          that.showlist()
        })
      },
    })


  },

  showlist: function () {
    $.post("api/service/lists", { fir_id: that.data.fir_id, sec_id: that.data.sec_id, cate_id: that.data.cate_id, issearch: that.data.issearch, search: that.data.search }, function (list) {
      that.setData({ list: list })
    })
  },



  setarealist: function () {
    $.post("api/index/getarea", null, function (list) {
      for (var i in list) {
        if (list[i].id == that.data.fir_id) {
          that.setData({ ['areaindex[0]']: i })
        }
      }
      that.setData({ ['area_list[0]']: list })
      that.setcitylist_core(that.data.fir_id, null, function (sublist) {
        for (var j in sublist) {
          if (sublist[j].id == that.data.sec_id) {
            that.setData({ ['areaindex[1]']: j })
          }
        }
      })
    })
  },
  setcitylist_core: function (id, e, cb) {
    $.post("api/index/getarea", {
      parent_id: id
    }, function (list) {
      that.setData({
        "area_list[1]": list
      })
      if (e != null) {
        that.setData({
          "areaindex[0]": e.detail.value,
          "areaindex[1]": 0
        })
      }
      if (cb != null) {
        cb(list)
      }
    })

  }, setcitylist: function (e) {
    if (e.detail.column != 0) {
      return
    }
    let id = that.data.area_list[0][e.detail.value].id

    that.setcitylist_core(id, e)
  },
  setcitylist_core: function (id, e, cb) {
    $.post("api/index/getarea", {
      parent_id: id
    }, function (list) {
      that.setData({
        "area_list[1]": list
      })
      if (e != null) {
        that.setData({
          "areaindex[0]": e.detail.value,
          "areaindex[1]": 0
        })
      }
      if (cb != null) {
        cb(list)
      }
    })
  }
  , setarea: function (e) {
    that.setData({
      "areaindex[0]": e.detail.value[0],
      "areaindex[1]": e.detail.value[1]
    })
    let sec_id = that.data.area_list[1][that.data.areaindex[1]].id
    let fir_id = that.data.area_list[0][that.data.areaindex[0]].id
    that.setData({ sec_id: sec_id, fir_id: fir_id })
    that.showlist()
  },
  setcate: function (e) {

    that.setData({ issearch: 0 })
    that.setData({ cate_id: e.currentTarget.dataset.id })
    that.showlist()
  },
  setinput: function (e) {
    that.setData({ search: e.detail.value })
  },
  tosearch: function (e) {
    that.setData({ issearch: 1 })
    that.showlist()
  }
}) 